//
//  ZJTabBarViewController.m
//  Copyright © 2016年 babbage. All rights reserved.
//

#import "ZJTabBarViewController.h"
#import "ZJNavigationController.h"
#import "ZJTabBar.h"

#import "VocalConcertVC.h"
#import "StarSquareVC.h"
#import "StarAnchorVC.h"
#import "StarTicket.h"
#import "MineVC.h"

@interface ZJTabBarViewController ()<ZJTabBarDelegate>

/**
 *  自定义的tabbar
 */
@property (nonatomic, weak) ZJTabBar *customTabBar;

@end

@implementation ZJTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //状态栏
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    //初始化tabbar
    [self setupTabbar];
    // 初始化所有的子控制器
    [self setupAllChildViewControllers];
    //隐藏阴影线
    [[UITabBar appearance] setShadowImage:[UIImage new]];
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
    // 设置顶部tabBar背景图片
    [self setupTabBarBackgroundImage];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //删除系统自带的UITabBarButton
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
}
/**
 *  初始化tabbar
 */
- (void)setupTabbar
{
    ZJTabBar *customTabBar = [[ZJTabBar alloc] init];
    customTabBar.frame = self.tabBar.bounds;
    customTabBar.delegate = self;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar = customTabBar;
}
/**
 *  监听tabbar按钮的改变
 *  @param from   原来选中的位置
 *  @param to     最新选中的位置
 */
- (void)tabBar:(ZJTabBar *)tabBar didSelectedButtonFrom:(NSInteger)from to:(NSInteger)to
{
    if (self.selectedIndex == to && to == 0 ) {//双击刷新制定页面的列表
//        UINavigationController *nav = self.viewControllers[0];
//        MyPetViewController *experience = nav.viewControllers[0];
//        [homeVC refrshUI];
    }
    self.selectedIndex = to;
}
/**
 *  初始化所有的子控制器
 */
- (void)setupAllChildViewControllers
{
    // 1.演唱会
    VocalConcertVC *vocalConcert = [[VocalConcertVC alloc] init];
    //    home.tabBarItem.badgeValue = @"N";
    [self setupChildViewController:vocalConcert title:@"演唱会" imageName:@"tabBar_icon1_Unselected" selectedImageName:@"tabBar_icon1_Selected"];
    
    // 2.星多坊
    StarSquareVC *starSquare = [[StarSquareVC alloc] init];
    //    project.tabBarItem.badgeValue = @"8";
    [self setupChildViewController:starSquare title:@"星多坊" imageName:@"tabBar_icon2_Unselected" selectedImageName:@"tabBar_icon2_Selected"];
    
    // 3.星主播
    StarAnchorVC *starAnchor = [[StarAnchorVC alloc] init];
    //    publicWelfare.tabBarItem.badgeValue = @"19";
    [self setupChildViewController:starAnchor title:@"星主播" imageName:@"tabBar_icon3_Unselected" selectedImageName:@"tabBar_icon3_Selected"];
    
    // 4.玩票
    StarTicket *starTicket = [[StarTicket alloc] init];
    //    publicWelfare.tabBarItem.badgeValue = @"19";
    [self setupChildViewController:starTicket title:@"玩票" imageName:@"tabBar_icon4_Unselected" selectedImageName:@"tabBar_icon4_Selected"];
    
    // 5.我
    MineVC *mine = [[MineVC alloc] init];
    //    mine.tabBarItem.badgeValue = @"93";
    [self setupChildViewController:mine title:@"我的" imageName:@"tabBar_icon5_Unselected" selectedImageName:@"tabBar_icon5_Selected"];
}

/**
 *  初始化一个子控制器
 *
 *  @param childVc           需要初始化的子控制器
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 1.设置控制器的属性
    childVc.title = title;
    // 设置图标
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    if (iOS7) {
        childVc.tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    } else {
        childVc.tabBarItem.selectedImage = selectedImage;
    }
    
    // 2.包装一个导航控制器
    ZJNavigationController *nav = [[ZJNavigationController alloc] initWithRootViewController:childVc];
    UINavigationBar *navBar = [UINavigationBar appearance];
    navBar.barTintColor = navBarBackgroundColor;
    nav.navigationBar.translucent = NO;
    nav.navigationBar.alpha = 1;
    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:18],NSFontAttributeName,nil];
    [nav.navigationBar setTitleTextAttributes:attributes];
    
    [self addChildViewController:nav];

    //当为星主播的时候 让其添加自己的按钮
    if ([title isEqualToString:@"星主播"]) {
        [self addCameraButton];
        return;
    }
    // 3.添加tabbar内部的按钮
    [self.customTabBar addTabBarButtonWithItem:childVc.tabBarItem];
    
}

#pragma mark ---- <添加视频采集按钮>
- (void)addCameraButton {
    
    UIButton *cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [cameraBtn setImage:[UIImage imageNamed:@"tabBar_icon3_Unselected"] forState:UIControlStateNormal];
    [cameraBtn setImage:[UIImage imageNamed:@"tabBar_icon3_Selected"] forState:UIControlStateHighlighted];
    
    // 自适应,自动根据按钮图片和文字计算按钮尺寸
    [cameraBtn sizeToFit];
    
    cameraBtn.center = CGPointMake(self.tabBar.width * 0.5, self.tabBar.height * 0.5 + 5);
    [cameraBtn addTarget:self action:@selector(clickCameraBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [self.customTabBar addSubview:cameraBtn];
    
}
#pragma mark ---- <点击了CameraBtn>
- (void)clickCameraBtn {
    
    StarAnchorVC *starAnchor = [[StarAnchorVC alloc] init];
    [self presentViewController:starAnchor animated:YES completion:nil];
    
}
-(void)setupTabBarBackgroundImage
{
//    UIImage *image = [UIImage imageNamed:@"tabbar_background"];
    UIImage *image = [UIImage imageNamed:@"tab_bg"];
    
    CGFloat top = 40; // 顶端盖高度
    CGFloat bottom = 40 ; // 底端盖高度
    CGFloat left = 100; // 左端盖宽度
    CGFloat right = 100; // 右端盖宽度
    UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
    // 指定为拉伸模式，伸缩后重新赋值
    UIImage *TabBgImage = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    self.tabBar.backgroundImage = TabBgImage;
    
    [[UITabBar appearance] setShadowImage:[UIImage new]];
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
